package problems.contest;

/**
 * @author habitplus
 * @since 2022/8/21 12:03
 */
public class BCT85 {
    /**
     * T1
     */
    public int minimumRecolors(String blocks, int k) {
        if (blocks == null || blocks.length() < 1 || k < 0) return -1;
        char[] chs = blocks.toCharArray();
        int n = chs.length;

        int j = 0;
        int ret = Integer.MAX_VALUE;
        for (int i = 0; i < n; ++i) {
            if (chs[i] == 'W') ++j;
            if (i + 1 >= k) {
                ret = Math.min(j, ret);
                j = chs[i + 1 - k] == 'W' ? j - 1 : j;
            }
        }
        return ret;
    }


    /**
     * T2
     */
    public int secondsToRemoveOccurrences(String s) {
        if (s == null || s.length() < 2) return 0;
        char[] chs = s.toCharArray();
        int ret = 0;
        while (f(chs)) ret++;

        return ret;
    }

    private boolean f(char[] chs) {
        int l = 0;
        boolean ret = false;
        while (l < chs.length - 1) {
            if (chs[l] == '0' && chs[l + 1] == '1') {
                chs[l] = '1';
                chs[l + 1] = '0';
                l++;
                ret = true;
            }
            l++;
        }
        return ret;
    }
}
